package com.haoqizhe.kernel.elasticsearch.controller;

import com.haoqizhe.kernel.elasticsearch.service.ICrudEsService;
import com.haoqizhe.kernel.core.entity.PageQuery;
import com.haoqizhe.kernel.core.entity.PageView;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.constraints.NotNull;
import java.io.Serializable;
import java.lang.reflect.ParameterizedType;
import java.util.Map;

/**
 * @author haoqizhe.li
 * @date 2018/9/5
 **/
@RestController
@Slf4j
public abstract class AbstractSearchController <T,PK extends Serializable>{

    /**
     * <p>注入service bean</p>
     * @return
     * @author haoqizhe.li
     * @date 2018/9/6
     **/
    protected abstract ICrudEsService<T, PK> getService();
    private final Class<T> entity;

    protected AbstractSearchController(){
        ParameterizedType pt = (ParameterizedType) this.getClass().getGenericSuperclass();
        entity = (Class<T>) pt.getActualTypeArguments()[0];
    }
    @ApiOperation(value = "根据主键ID获取单条数据", notes = "根据主键ID获取单条数据")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id",value = "主键",required = true,dataType = "String",paramType = "query")
    })
    @GetMapping("/get")
    public T get(@NotNull(message = "id不能为空") PK id){
        return this.getService().get(id);
    }

    @ApiOperation(
            value = "分页按参数搜索数据",
            notes = "分页按参数搜索数据"
    )
    @PostMapping("/page")
    public PageView<T> page(@RequestBody PageQuery<Map<String, Object>> pageQuery) {
        return this.getService().findBy(pageQuery);
    }
    @ApiOperation(
            value = "分页搜索数据",
            notes = "分页搜索数据"
    )
    @PostMapping("/search")
    public PageView<T> search(String word,Integer pageNum,Integer pageSize){
        return this.getService().findBy(word, pageNum,pageSize);
    }
}
